export default {
  data() {
    return {
      scrollTop: 0,
    }
  },
  methods: {
    scroll() {
      this.scrollTop = document.documentElement.scrollTop
    },
  },
  mounted() {
    window.addEventListener('scroll', this.scroll)
    // 初始渲染的时候也要应用下初始值
    document.documentElement.scrollTop = this.scrollTop
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.scroll)
  },
  activated() {
    // 从缓存中激活的时候也要监听
    window.addEventListener('scroll', this.scroll)
    this.$nextTick(() => (document.documentElement.scrollTop = this.scrollTop))
  },
  deactivated() {
    window.removeEventListener('scroll', this.scroll)
  },
}
